package com.zlll.winner.service.lhtUser;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zlll.winner.business.model.lhtUser.LhtUser;
import com.zlll.winner.business.model.lhtUser.LhtUserIdentity;
import com.zlll.winner.business.service.lhtUser.ILhtUserIdentityService;
import com.zlll.winner.enums.CommonEnum;
import com.zlll.winner.mapper.lhtUser.LhtUserMapper;
import com.zlll.winner.business.service.lhtUser.ILhtUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlll.winner.util.SaltUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author qsx
 * @since 2019-09-05
 */
@Service
public class LhtUserServiceImpl extends ServiceImpl<LhtUserMapper, LhtUser> implements ILhtUserService {

    @Autowired
    private LhtUserMapper lhtUserMapper;
    @Autowired
    private ILhtUserIdentityService userIdentityService;

    /**
     * 获取用户信息
     * @param phone
     * @return
     */
    @Override
    public LhtUser getLhtUser(String phone) {
        List<LhtUser> lhtUsers = lhtUserMapper.getLhtUserByPhone(phone);
        if (lhtUsers.size() > 0) {
            return lhtUsers.get(0);
        }
        return null;
    }

    /**
     * 注册功能
     * @param lhtUser
     * @return
     */
    @Override
    @Transactional
    public LhtUser register(LhtUser lhtUser) {
        if (lhtUser.getPassword() != null) {
            String salt = SaltUtil.getSalt();
            String encodePassword = DigestUtils.md5Hex(lhtUser.getPassword() + salt);
            lhtUser.setPassword(encodePassword);
            lhtUser.setSalt(salt);
        }
        lhtUser.setCreateTime(System.currentTimeMillis());
        lhtUser.setUpdateTime(System.currentTimeMillis());

        lhtUser.setIsDel(0);
        save(lhtUser);
        return lhtUser;
    }

    /**
     * 验证密码
     * @param user
     * @param password
     * @return
     */
    @Override
    @Transactional
    public Boolean validateUserPassword(LhtUser user, String password) {
        String encodePassword = DigestUtils.md5Hex(password + user.getSalt());
        Boolean result = StringUtils.equals(user.getPassword(), encodePassword);
        return result;
    }

    /**
     * 更新密码
     * @param user
     * @param newPassWord
     */
    @Override
    @Transactional
    public void changePassword(LhtUser user, String newPassWord) {
        String salt = SaltUtil.getSalt();
        String encodePassword = DigestUtils.md5Hex(newPassWord + salt);
        user.setUpdateTime(System.currentTimeMillis());
        user.setSalt(salt);
        user.setPassword(encodePassword);
        lhtUserMapper.updateById(user);
    }

    /**
     * 分页查询蓝海豚用户信息
     * @param phone
     * @param nickname
     * @param identityId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public IPage<LhtUser> getLhtUser(String phone, String nickname, Long identityId, Integer pageNo, Integer pageSize) {
        IPage<LhtUser> page = new Page<LhtUser>(pageNo, pageSize);
        List<LhtUser> lhtUsers = lhtUserMapper.getLhtUserByCondition(phone, nickname, identityId, page);
        page.setRecords(lhtUsers);
        return page;
    }

    /**
     * 更新用户的身份
     * @param userId
     * @param identityIds
     * @return
     */
    @Override
    @Transactional
    public Boolean updateLhtUserIdentity(Long userId, String identityIds) {
        Long time = System.currentTimeMillis();
        Boolean result = userIdentityService.deleteLhtUserIdentity(userId);
        if (null != identityIds && identityIds.length() > 0) {
            String[] ids = identityIds.split(",");
            List<LhtUserIdentity> lhtUserIdentities = new ArrayList<>();
            for (String id : ids) {
                if (!id.equals("")) {
                    LhtUserIdentity lhtUserIdentity = new LhtUserIdentity();
                    lhtUserIdentity.setIdentityId(Long.parseLong(id));
                    lhtUserIdentity.setUserId(userId);
                    lhtUserIdentity.setIsDel(0);
                    lhtUserIdentity.setCreateTime(time);
                    lhtUserIdentities.add(lhtUserIdentity);
                }
            }
            userIdentityService.saveBatch(lhtUserIdentities);
        }
        return result;
    }

    /**
     * 新增用户信息
     * @param user
     * @param identityIds
     * @param name
     */
    @Override
    @Transactional
    public Long addUser(LhtUser user, Long[] identityIds, String name) {
        String salt = SaltUtil.getSalt();
        String newPassWord = user.getPassword()==null ? "lht@123":user.getPassword();
        user.setCreateTime(new Date().getTime());
        user.setCreateUser(name);
        user.setSalt(salt);
        user.setIsDel(CommonEnum.DelEnum.UNDEL.val());
        user.setPassword(DigestUtils.md5Hex(newPassWord + salt));
        lhtUserMapper.insert(user);
        LhtUserIdentity lhtUserIdentity = null;
        List<LhtUserIdentity> lhtUserIdentities = new ArrayList<>();
        for (Long id : identityIds){
            lhtUserIdentity = new LhtUserIdentity();
            lhtUserIdentity.setIdentityId(id);
            lhtUserIdentity.setUserId(user.getId());
            lhtUserIdentity.setIsDel(CommonEnum.DelEnum.UNDEL.val());
            lhtUserIdentity.setCreateTime(new Date().getTime());
            lhtUserIdentity.setCreateUser(name);
            lhtUserIdentities.add(lhtUserIdentity);
        }
        userIdentityService.saveBatch(lhtUserIdentities);
        return user.getId();
    }

}
